$(function () {
    getUserInfo()
   
    $('#btnLogout').on('click', function () {
        layui.layer.confirm('你确定要退出码？', { icon: 3, title: '提示' }, function (index) {
            //do something
            localStorage.removeItem('token')
            location.href = '/login.html'

            layui.layer.close(index);
        });
    });

});

function getUserInfo() {
    $.ajax({
        type: 'get',
        url: '/my/userinfo',
        success: function (res) {
            if (res.status !== 0) {
               return layui.layer.msg('获取用户身份失败！')
            }
            renderAvatar(res.data)
        }

    });
};

function renderAvatar(fan) {
    var name = fan.nickname || fan.username;
    $('#welcome').html('欢迎&nbsp;&nbsp;' + name);
    if (fan.user_pic !== null) {
        $('.layui-nav-img').attr('src', fan.user_pic).show()
        $('.text-vavtar').hide();
    }else {
        
        $('.text-vavtar').html(name[0].toUpperCase()).show()
        $('.layui-nav-img').hide();
    } 
};